//
//  MyNavigationController.m
//  BMProject
//
//  Created by 王艳茹 on 15/11/14.
//  Copyright © 2015年 MengHuan. All rights reserved.
//

#import "UIColor+NN.h"
#import "MyNavigationController.h"
//#import "UIImage+Extension.h"
@interface MyNavigationController()<UIGestureRecognizerDelegate>
@end

@implementation MyNavigationController

+ (void)initialize
{
    // 设置整个项目所有item的主题样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    // 设置普通状态
    // key：NS****AttributeName
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置不可用状态
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
    disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    
    //这里我们设置的是颜色，还可以设置shadow等，

     [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.barTintColor = [UIColor colorWithHexString:@"BB3535"];
//    [self.navigationBar setBackgroundColor:[UIColor colorWithHexString:@"BB3535"]];
    NSDictionary *dict = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    [self.navigationBar setTitleTextAttributes:dict];
 
//    [self.navigationBar setBackgroundImage: [UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
//     [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation"] forBarMetrics:UIBarMetricsDefault];
    
    //去除navBar底部线
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

    [self createBackButton];
//    [self addSwipeRecognizer];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    {
         if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
      {
            return NO;
            }
       {
           return YES;
           }
}
-(void)popself
{
    // 因为self本来就是一个导航控制器，self.navigationController这里是nil的

    [self popViewControllerAnimated:YES];
}
-(UIBarButtonItem*) createBackButton

{
    
    NSString *titile = @"返回";
    //    UIViewController *vc = self.viewControllers[self.viewControllers.count-2];
    //    titile = vc.title;
    
    //
    float width = 80.0f;
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, width, 40);
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(-15, 0, 25, 40);
    [left setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [view addSubview:left];
    [left addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    UIButton *label = [UIButton buttonWithType:UIButtonTypeCustom];
    label.frame = CGRectMake(CGRectGetMaxX(left.frame), 0, width-CGRectGetWidth(left.frame), 40);
    [label setTitle:titile forState:UIControlStateNormal];
    [label setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    label.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [label addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:label];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    return backItem;
    return  [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(popself)];
    
}

#pragma mark 添加右滑手势
- (void)addSwipeRecognizer
{
//    // 初始化手势并添加执行方法
    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(return)];
    
    // 手势方向
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    
    // 响应的手指数
    swipeRecognizer.numberOfTouchesRequired = 1;
    
    // 添加手势
    [[self view] addGestureRecognizer:swipeRecognizer];
}

#pragma mark 返回上一级
- (void)return
{
    // 最低控制器无需返回
    if (self.viewControllers.count <= 1) return;
    
    // pop返回上一级
    [self popViewControllerAnimated:YES];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{
    viewController.hidesBottomBarWhenPushed = self.viewControllers.count>=1;
    
    [super pushViewController:viewController animated:animated]; 
 
    if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) {
        
        viewController.navigationItem.leftBarButtonItem =[self createBackButton];
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        viewController.navigationController.interactivePopGestureRecognizer.delegate = (id)viewController;

        return;
        
    }
   


}


@end
